

import 'package:adaptive_theme/adaptive_theme.dart';
import 'package:flustars/flustars.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';

import 'package:flutter_switch/flutter_switch.dart';
import 'package:get/get.dart';
import 'package:smart_freezer/core/resources/app_config.dart';
import 'package:smart_freezer/core/resources/icon_names.dart';
import 'package:smart_freezer/core/resources/routers.dart';
import 'package:smart_freezer/core/utils/lc_event_bus/call.dart';
import 'package:smart_freezer/core/utils/lc_event_bus/notify.dart';
import 'package:smart_freezer/core/utils/local_storage.dart';
import 'package:smart_freezer/core/utils/tr_extension.dart';

import '../../../generated/locale_keys.g.dart';

class SettingModel extends StatefulWidget {
  const SettingModel({Key? key}) : super(key: key);

  @override
  State<SettingModel> createState() => _SettingModelState();
}

class _SettingModelState extends State<SettingModel> {

  List _dataList = [LocaleKeys.set_up_mode_dark.trc(), LocaleKeys.set_up_mode_system.trc()];
  int _selectIndex = LocalStorage.getThemeMode() - 1;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        // backgroundColor: Colors.white,
        elevation: 0,
        leadingWidth: 50,
        leading: InkWell(
          onTap: () {
            Get.back();
          },
          child: Container(
            padding: EdgeInsets.fromLTRB(0, 9, 0, 9),
            child: Icon(Icons.arrow_back_ios_rounded),
          ),
        ),
        title: Text(LocaleKeys.set_up_setting_mode.trc()),
        centerTitle: true,
      ),
      body: Container(
        width: MainCommon.screenWidth,
        padding: EdgeInsets.fromLTRB(0, 10, 0, 0),
        color: _selectIndex == 0 ? Colors.black : MainCommon.bgGreyColor,
        child: ListView.builder(
          itemCount: _dataList.length,
            itemBuilder: (context, index) {
              final title = _dataList[index];
              return InkWell(
                onTap: () {
                  setState(() {
                    _selectIndex = index;

                    _dealwithMode();
                  });
                },
                child: Container(
                  color: LocalStorage.getThemeMode() == 1 ? MainCommon.darkBlueMaterialColor : Colors.white,  // Color(0xFFF1F3F8),
                  padding: EdgeInsets.symmetric(horizontal: 15),
                  height: 50,
                  child: Column(
                    children: [
                      Expanded(
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          crossAxisAlignment: CrossAxisAlignment.center,
                          children: [
                            Text(title, style: TextStyle(fontWeight: FontWeight.bold),),
                            FlutterSwitch(
                              activeColor: Colors.greenAccent,
                              width: 40,
                              height: 20.0,
                              toggleSize: 20.0,
                              value: _selectIndex == index,
                              borderRadius: 20.0,
                              padding: 1.0,
                              onToggle: (val) {
                                setState(() {
                                  _selectIndex = index;

                                  _dealwithMode();

                                });
                              },
                            ),
                          ],
                        ),
                      ),
                      Divider(
                        height: 1,
                        thickness: 0,
                        indent: 5,
                        endIndent: 5,
                        // color: Color(0xFFD9D9D9),
                      ),

                    ],
                  ),
                ),
              );
            }
        ),
      ),
    );
  }

  _dealwithMode() {
    LocalStorage.setThemeMode(_selectIndex + 1);
    if(_selectIndex == 1){
      AdaptiveTheme.of(context).setSystem();
    }else if(_selectIndex == 0){
      AdaptiveTheme.of(context).setDark();
    }

    Call.dispatch(Notify.dark_mode, data: "");
  }

}
